package domain.service.base;

import core.util.ValidationUtil;
import domain.dao.base.UserInfoDao;
import domain.entity.base.AgentInfo;
import domain.entity.base.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * UserInfoService
 */
@Service
public class UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;

    @Transactional(value = "base", rollbackFor = Exception.class)
    public void getAgentUserInfo() {
        try {
            List<UserInfo> userInfoList = new ArrayList<UserInfo>();
//            AgentInfo agentInfo = ValidationUtil.getAgentInfo();
//            if (agentInfo != null) {
//                if (agentInfo.getAllow_userinfos() != null && agentInfo.getAllow_userinfos().getUser() != null && agentInfo.getAllow_userinfos().getUser().size() > 0) {
//                    UserInfo userInfo = null;
//                    for (UserInfo user : agentInfo.getAllow_userinfos().getUser()) {
//                        userInfo = ValidationUtil.getUserInfo(user.getUserid());
//                        userInfoList.add(userInfo);
//                    }
//                }
//                if (agentInfo.getAllow_partys() != null && agentInfo.getAllow_partys().getPartyid() != null && agentInfo.getAllow_partys().getPartyid().length > 0) {
//                    List<UserInfo> userInfos = null;
//                    for (String partyid : agentInfo.getAllow_partys().getPartyid()) {
//                        userInfos = ValidationUtil.getUserInfoByPartyId(partyid);
//                        userInfoList.addAll(userInfos);
//                    }
//                }
//            }
            if (userInfoList.size() > 0) {
                userInfoDao.deleUsers();
                userInfoDao.addList(userInfoList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<UserInfo> getEventUsers() {
        return userInfoDao.getEventUsers();
    }

    public UserInfo getUserInfoById(String code) {
        return userInfoDao.getUserInfoById(code);
    }

    public boolean getAdmin(String code) {
        UserInfo user = userInfoDao.getAdmin(code);
        if (user != null) {
            return true;
        }
        return false;
    }

    public void addEventUsers(String eventIds,String adminIds){
        String userId[] = eventIds.split(",");
        if (!StringUtil.isNullOrEmpty(eventIds)) {
            List<String> userids = new ArrayList<String>();
            for (int i = 0; i < userId.length; i++) {
                userids.add(userId[i]);
            }
            userInfoDao.addEventUsers(userids);
        }
        String adminId[] = adminIds.split(",");
        if (!StringUtil.isNullOrEmpty(adminIds)) {
            List<String> userids = new ArrayList<String>();
            for (int i = 0; i < adminId.length; i++) {
                userids.add(adminId[i]);
            }
            userInfoDao.addAdmins(userids);
        }
    }

    public List<UserInfo> getUserCheck() {
        return userInfoDao.getUserCheck();
    }

}
